import Vue from 'vue'
import VueRouter from 'vue-router'
// import LoginView from '../views/LoginView.vue'
// import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    // component: LoginView // 最后可以不用加符号
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/home/welcome',
    // component: HomeView,
    component: () => import('../views/HomeView.vue'),
    children: [
      {
        path: '/home/welcome',
        name: 'welcomeView',
        component: () => import('../components/WelcomeView.vue')
      },
      {
        path: '/users',
        name: 'UserView',
        component: () => import('../components/user/UserView.vue')
      },
      {
        path: '/rights',
        name: 'rightsView',
        component: () => import('../components/power/rightsView.vue')
      },
      {
        path: '/roles',
        name: 'rolesView',
        component: () => import('../components/power/rolesView.vue'),
        beforeEnter (to, from, next) {
          // console.log(to)
          // console.log(from)
          console.log('是会员吗');

          next()
        }
      },
      {

        path: '/categories',
        name: 'categoriesView',
        component: () => import('../components/shopping/categoriesView.vue')

      }

    ]
    // meta: { requiresAuth: true }
  }

  // { path: '/', redirect: '/home' }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (token) {
    next()
  } else {
    if (to.path === '/') {
      next()
    } else {
      next('/')
    }
  }
})

export default router
